//
//  XYJLXViewController.m
//  徙游记-01
//
//  Created by hcios on 2017/7/10.
//  Copyright © 2017年 鹿林. All rights reserved.
//

#import "XYJLXViewController.h"
#import "XYJLXModel.h"
#import "XYJLXTableViewCell.h"
#import "XYJLXResultViewController.h"

@interface XYJLXViewController ()<UITableViewDelegate,UITableViewDataSource>{
    
    NSMutableArray *_dataArr;
    UITableView *_tableView;
    NSInteger page;
}

@end

@implementation XYJLXViewController

-(instancetype)init{
    if (self = [super init]) {
        _dataArr = [[NSMutableArray alloc] init];
        page = 1;
    }
    return self;
}

-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    NSLog(@"出现");
    self.navigationController.navigationBarHidden = NO;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initTable];
    [_tableView.mj_header beginRefreshing];
    self.title = [NSString stringWithFormat:@"%@旅行地",_cityName];
}


-(void)initTable{
    
    _tableView = [[UITableView alloc] initWithFrame:RECT_MAKE(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.showsVerticalScrollIndicator = YES;
    //刷新
    __weak typeof(self) weakSelf = self;
    
    _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [weakSelf refreshGetData:YES];
    }];
    _tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        [weakSelf refreshGetData:NO];
    }];
    [self.view addSubview:_tableView];
}

-(void)refreshGetData:(BOOL)isRefresh{
    
    isRefresh ? page = 1 : page++;
    NSLog(@"当前页面数量:%ld",(long)page);
    NSString *urlString = [NSString stringWithFormat:@"http://chanyouji.com/api/destinations/attractions/%@.json?per_page=20",_selfID];
    NSDictionary *param = @{@"page":@(page)};
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager GET:urlString parameters:param progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if (isRefresh) {
            [_dataArr removeAllObjects];
            _tableView.mj_footer.state = MJRefreshStateIdle;
        }
        
        if ([responseObject count] > 0) {
            for (NSDictionary *dict in responseObject) {
                XYJLXModel *model = [[XYJLXModel alloc] initWithDict:dict];
                [_dataArr addObject:model];
            }
        }
        if (_dataArr.count < 10) {
            [_tableView.mj_footer endRefreshingWithNoMoreData];
        }
        [_tableView reloadData];
        
        if (!isRefresh && [responseObject count] == 0) {
            [_tableView.mj_footer endRefreshingWithNoMoreData];
        }else [self endRefresh];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"网络请求失败");
    }];
}

-(void)endRefresh{
    if(page == 1){
        [_tableView.mj_header endRefreshing];
    }else [_tableView.mj_footer endRefreshing];
}




- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _dataArr.count;
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    XYJLXTableViewCell *cell = [XYJLXTableViewCell cellModelWithTableViewCell:tableView];
    XYJLXModel *model = _dataArr[indexPath.row];
    cell.model = model;
    return cell;
    
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 100;
}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return CGFLOAT_MIN;
}



- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return CGFLOAT_MIN;
}




-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSLog(@"%ld%ld",(long)[indexPath section],(long)[indexPath row]);
    
    XYJLXResultViewController *VC = [[XYJLXResultViewController alloc] init];
    XYJLXModel *model = _dataArr[indexPath.row];
    VC.selfID = model.lxd_id;
    [self.navigationController pushViewController:VC animated:YES];
}


@end
